<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

/**
 * Router Class
 *
 * Extends CI Router
 */

class SP_Router extends CI_Router {
	
	/* Constructor */
	function SP_Router()
	{
		// Needed for error pages
		session_start();

		parent::CI_Router();
	}
	
	// --------------------------------------------------------------------
	
	function _set_routing()
	{		
		// Are query strings enabled in the config file?
		// If so, we're done since segment based URIs are not used with query strings.
		if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
		{
			$this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));

			if (isset($_GET[$this->config->item('function_trigger')]))
			{
				$this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
			}
			
			return;
		}
		
		// Load the routes.php file.
		@include(APPPATH.'config/routes'.EXT);
		$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
		unset($route);

		// Set the default controller so we can display it in the event
		// the URI doesn't correlated to a valid controller.
		$this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);	
		
		// Fetch the complete URI string
		$this->uri->_fetch_uri_string();
	
		// Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
		if ($this->uri->uri_string == '')
		{
			if ($this->default_controller === FALSE)
			{
				show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
			}
			
			$this->set_directory($this->default_controller);
			$this->set_class($this->default_controller);
			$this->set_method('index');
			$this->_set_request(array($this->default_controller));
			
			// re-index the routed segments array so it starts with 1 rather than 0
			$this->uri->_reindex_segments();
			
			log_message('debug', "No URI present. Default controller set.");
			return;
		}
		unset($this->routes['default_controller']);
		
		// Do we need to remove the URL suffix?
		$this->uri->_remove_url_suffix();
		
		// Compile the segments into an array
		$this->uri->_explode_segments();
		
		// Parse any custom routing that may exist
		$this->_parse_routes();		
		
		// Re-index the segment array so that it starts with 1 rather than 0
		$this->uri->_reindex_segments();
	}
	
	// --------------------------------------------------------------------
	
	function _validate_request($segments)
	{
		// Check for error flag
		if (isset ($_SESSION['error']) )
		{
			$this->set_directory('error');
			$segments = array('error', 'placebo');
						
			return $segments;
		}
		
		// CSS request
		if ($segments[0] == 'css')
		{
			define('CSS'	, $segments[1]);			
			$segments = array('home');
		}

		// JS Request
		if ($segments[0] == 'js')
		{
			define('JS'		, $segments[1]);
			$segments = array('home');
		}

		// Not an error request, let's figure out where to go
		if (count($segments) < 2)
		{
			//Calling the index function of a controller of the same directory...
			//We'll cheat and just set our segment
			$segments[1] = $segments[0];
		}

		// Does the requested controller exist as a full path including the directory?
		if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[1].EXT))
		{
			//Set the directory
			$this->set_directory($segments[0]);
			
			//Drop the directory segment
			$segments = array_slice($segments, 1);
			return $segments;
		}
		
		//Ok, that didn't work, let's try duplicating segment 0, maybe it's the same ;).
		if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[0].EXT))
		{
			//Set the directory
			$this->set_directory($segments[0]);
			
			//We cheated so there's nothing to drop
			return $segments;
		}
	
		// Can't find the requested controller...
		show_404();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 *  Set the directory name
	 *
	 * @access	public
	 * @param	string
	 * @return	void
	 */	
	function set_directory($dir)
	{
		$this->directory = $dir;
	}

	// --------------------------------------------------------------------
	
	/**
	 *  Fetch the sub-directory (if any) that contains the requested controller class
	 *
	 * @access	public
	 * @return	string
	 */	
	function fetch_directory()
	{
		return $this->directory.'/';
	}
}
?>